<form class="form-horizontal" role="form" id="roleForm">
	<input type="hidden" name="doCode"  value="roleSave"/>
	<input type="hidden" name="method"  value="save"/>
	<!-- Custom Tabs -->
	<div class="nav-tabs-custom">
		<div role="toolbar" class="btn-toolbar">
			<div class="btn-group">
				<button aria-label="Left Align" class="btn btn-default" type="button" onclick="addRowRole();"><span aria-hidden="true" class="glyphicon glyphicon-plus"></span>增加</button>
				<button aria-label="Center Align" class="btn btn-default" type="button" onclick="delRole(0);"><span aria-hidden="true" class="glyphicon glyphicon-minus	"></span>删除</button>
				<button aria-label="Right Align" class="btn btn-default" type="button" onclick="saveRole(0);"><span aria-hidden="true" class="glyphicon glyphicon-pencil"></span>修改</button>
				<!-- button aria-label="Justify" class="btn btn-default" type="button" onclick="popRoleActs();"><span aria-hidden="true" class="glyphicon glyphicon-cog"></span>权限设置</button -->
			</div>
		</div>					            
		<div class="tab-content">
			<div class="tab-pane active" id="role_tab">
				<table id="roleTable" class="table table-bordered table-hover" >
					<thead>
						<tr>
							<th width="25%">角色名称</th><th width="15%">角色标识</th>
							<th width="10%">所属机构</th><th width="15%">创建人</th>
							<th width="15%">创建时间</th><th width="250">操作</th>
						</tr>
					</thead>
					<tbody id="roleList"></tbody>
				</table>
			</div>
				<!-- /.tab-pane -->
		</div>
				<!-- /.tab-content -->
	</div>
</form>
<script type="text/template" id="roleListTemplate">
   <? for ( var i = 0; i < rows.length; i++) {?>
		<tr id="roleRow_<?=i+1?>">
		<th style="display:none;">
			<?=i+1?>
		</th>
		<td>
			<input type="text" class="form-control" value="<?=rows[i].roleCname?>" id="roleCname_<?=i+1?>" name="roleCname"  / >
		</td>
		<td>
			<input type="text" class="form-control" value="<?=rows[i].roleEname?>" id="roleEname_<?=i+1?>" name="roleEname"  / >
		</td>
		<td class="col-xs-2">
			<input type="hidden" class="form-control" value="<?=orgId?>" id="orgId_<?=i+1?>" name="orgId"  / >
            <input type="hidden" class="form-control" value="<?=cname?>" id="roleRemark_<?=i+1?>" name="roleRemark"  / >
			<input type="hidden" class="form-control" value="<?=rows[i].roleId?>" id="roleId_<?=i+1?>" name="roleId"  / >
			<?if(rows[i].roleRemark){?><?=rows[i].roleRemark?><?}else{?>--<?}?>
		</td>
		<td>
			<input type="text" value="<?=rows[i].createUser?>" class="form-control" id="createUser_<?=i+1?>" name="createUser" readonly / >
		</td>
		<td><?=getFormatDate(rows[i].createTime)?></td>
		<td>
			<button type="button" class="btn btn-success btn-xs"  onclick="saveRole(<?=i+1?>);">保存<button>
            <button type="button" class="btn btn-success btn-xs" onclick="popRoleActs(<?=i+1?>);">权限</button>
		</td>
	</tr>
  <?}?>
</script>

<script type="text/template" id="addRowRole">
	<tr id="roleRow_<?=index?>">
		<td>
			<input type="text" class="form-control" value="" id="roleCname_<?=index?>"  name="roleCname" / >
		</td>
		<td>
			<input type="text" class="form-control" value="" id="roleEname_<?=index?>"  name="roleCname"/ >
		</td>
		<td class="col-xs-2">
			<input type="hidden" class="form-control" value="<?=roleId?>" id="roleId_<?=index?>" name="roleId"  / >
			<input type="hidden" class="form-control" value="<?=orgId?>" id="orgId_<?=index?>" name="orgId"  / >
			<input type="text" class="form-control" value="<?=cname?>" id="roleRemark_<?=index?>" name="roleRemark"  readonly/ >
		</td>
		<td>
			<input type="text" value="" class="form-control" name="createUser" id="createUser_<?=index?>" / >
		</td>
		<td>
			<input type="text" value="" class="form-control" name="createTime" id="createTime_<?=index?>" readonly/ >
		</td>
		<td>
			<button type="button" class="btn btn-success" onclick="saveRole(<?=index?>);">保存</button>
		</td>
	</tr>				
</script>
<script>
var pro_ctx;
function getRoleList(orgId,fatherId,ctx){
    pro_ctx = ctx;
	deleteAllRow("roleTable");
	var paramsData={};
	paramsData.doCode="roleList";
	paramsData.orgId=orgId;
	paramsData.fatherId=fatherId;
	paramsData.method="roleActData";
	var result = roleAjax(common_list_url,'get','json',paramsData);
	if(result){
			   // console.log(myData);
				index=result.total;
				result.orgEname=ename;
				var data = baidu.template("roleListTemplate",result); 
				$("#roleList").append(data);
		   }
  }
  
  
function addRowRole(){
	var myData = {};
	myData.cname = cname;
	myData.orgId = orgId;
	myData.index = index+1;
	index = index+1;
	var data = baidu.template("addRowRole",myData); 
	$("#roleList").append(data);
}

function saveRole(num){
	//如果num为0，则说明是没有选中数据便点击了修改。
	if(num==0){ num = deptNum;}
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
		var paramsData={};
		paramsData.roleCname = $("#roleCname_"+num).val();
		paramsData.roleEname = $("#roleEname_"+num).val();
		paramsData.delStatus = $("#orgId_"+num).val();
		paramsData.roleRemark = $("#roleRemark_"+num).val();
		paramsData.doCode="roleSave";
		paramsData.method="save";
		var roleId =$("#roleId_"+num).val();
		if(roleId !=null && roleId!=""){
			paramsData.roleId = roleId;
		}
		var ocname=$("#roleRemark_"+num).val();
		if(ocname !=null && ocname!=""){
		     paramsData.roleRemark = cname;
		}
		var result = roleAjax(common_submit_url,'post','json',paramsData);
		if(result){
		      	layer.alert(result.msg, {closeBtn: 0});
		    	if(result.flag=='T'){
		    	   getRoleList(orgId,fatherId);
		    	}
		  	}
	}
}

function delRole(num){
	if(num==0){num = deptNum;}//如果num为0，则说明是没有选中数据便点击了修改。
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
		var paramsData={};
		paramsData.doCode="roleDel";
		var deptId =$("#roleId_"+num).val();
		if(deptId !=null && deptId!=""){paramsData.roleId = deptId;}
		var myData = roleAjax(common_submit_url,'post','json',paramsData);
		if(myData){
		   // console.log(myData);
		      	layer.alert(myData.msg, {  closeBtn: 0 });
		    	if(myData.flag=='T'){
		    	   getRoleList(orgId,fatherId);
		    	}
		  	}
	}
}

function popRoleActs(num){
	if(num==0){num = deptNum;}//如果num为0，则说明是没有选中数据便点击了修改。
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
	    $('#role_modal').modal('hide');
	    var deptId =$("#roleId_"+num).val();
		$("#roleActsDiv").load(ctx+"/template/uct/role-acts.html",function(){getRoleListData(deptId,"0");
		$('#role_acts_modal').modal('show');
		$('#role-actsTable tbody').on('click','tr', function (e) {
		     if ( $(this).hasClass('selected') ) {
		        }  else {
		            $('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		        }
		     deptNum = $('tr.selected th').eq(0).html();
		  });
		});
	}
}


function roleAjax(url,type,dataType,data,formId){
  var formId=formId;var result;
  var _data = data;
  if(formId){
    _data = $('#'+formId).serialize();
  }
  $.ajax({url: url,type: type ,data:_data ,async:false,dataType:dataType,
 	success: function(mydata){console.log(result);
 	result = mydata;} });
 	return result;
}

function getFormatDate(l, pattern) {
        var date =new Date(l);
        if (pattern == undefined) {
            pattern = "yyyy-MM-dd hh:mm:ss";
        }
        return date.format(pattern);
    }
Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S": this.getMilliseconds()
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }
</script>